package edu.nyu.hps.architecture;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * This class acts as a BOT program for the players to test their programs 
 * @author hiralrajani
 *
 */
public class MyClient {

	// Initializations
	Socket client = null;
	PrintWriter out = null;
	BufferedReader in = null;
	boolean connected = false;
	String response = null, fromServer, fromUser;
	int player = 0, x, y, ox, oy, port = 4444;
	Player character = null;

	public MyClient(int port){
		this.port = port;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length < 1){
			System.out.println("Usage: \"java -jar Bot.jar <port>\" ");
			System.exit(1);
		}
		MyClient client = new MyClient(Integer.parseInt(args[0]));
		client.compute();
	}

	/**
	 * The actually functionality
	 */
	public void compute() {

		// Trying to establish connection to the server
		// and waiting for the ACK from the server
		try {
			client = new Socket("localhost", port);
			out = new PrintWriter(client.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(client
					.getInputStream()));

			// Try to JOIN till the server sends an ACK
			while (!connected) {
				response = null;
				out.println("JOIN:Hiral");

				// Waiting for the ACK or NACK responses from the server
				while ((response = in.readLine()) == null) {
				}
				if (response.charAt(0) == 'A') {
					System.out.println("Socket Acknowledged!");
					String[] input = response.split(":");
					player = input[1].equals("0") ? 0 : 1;
					x = Integer.parseInt(input[2].split(",")[0]);
					y = Integer.parseInt(input[2].split(",")[1]);
					System.out.println("I have to start playing at: (" + x + "," + y + ")");
					connected = true;
				}
			}

		} catch (UnknownHostException e) {
			System.err.println("Don't know about host: localhost.");
			System.exit(1);
		} catch (IOException e) {
			System.err
					.println("Couldn't get I/O for the connection to: localhost.");
			System.exit(1);
		}

		if(player==0){
			character = new Cat(x,y);
		}
		else{
			character = new Mouse(x,y);
		}
		response = null;
		while (true) {
			try {
				while ((response = in.readLine()) == null) {
				}
				if (response.charAt(0) == 'P') {
					String process[] = response.split(":");
					if (player == 0) {
						x = Integer.parseInt(process[2].split(",")[0]);
						y = Integer.parseInt(process[2].split(",")[1]);
						ox = Integer.parseInt(process[3].split(",")[0]);
						oy = Integer.parseInt(process[3].split(",")[1]);
						character.setOpponentLoc(ox, oy);
						out.println(character.clientMove(x, y));
					} else {
						x = Integer.parseInt(process[3].split(",")[0]);
						y = Integer.parseInt(process[3].split(",")[1]);
						ox = Integer.parseInt(process[2].split(",")[0]);
						oy = Integer.parseInt(process[2].split(",")[1]);
						character.setOpponentLoc(ox, oy);
						out.println(character.clientMove(x, y));
					}
				} else if (response.charAt(0) == 'S') {
					break;
				}
			} catch (IOException e) {
				System.out.println("No I/O from yhe server");
			}
		}

		// Closing all the open handles
		try {
			out.close();
			in.close();
			client.close();
		} catch (IOException e) {
			System.out.println("Could not close all the handles");
		}
	}

	public String catMoves(int x, int y) {
		return "MOVE:" + x + "," + (++y) + ":" + x + "," + (++y) + ":" + (++x)
				+ "," + (y) + ":" + (++x) + "," + (y);
	}

	public String mouseMoves(int x, int y) {

		return "MOVE:" + (++x) + "," + y;
	}
}
